package cn.sdc.bugKiller.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;

import cn.sdc.bugKiller.service.ContextService;
import cn.sdc.bugKiller.util.DbUtil;

public class BaseDao {
	protected DbUtil dbutil;
	
	public BaseDao(){
		dbutil = DbUtil.getInstance();
	}
	
	public void commit(){
		dbutil.commit();
	}
	
	public void rollback(){
		dbutil.rollback();
	}
	
	public static StringBuffer getSqlFromFile(String path){
		String fileName = ContextService.resourceRootPath + File.separator + "sql"+ File.separator+path;
        File file = new File(fileName);
        StringBuffer result = new StringBuffer();
        if (file.isFile() == false) {
            return result;
        }
        BufferedReader reader = null;
        try {
            FileInputStream fis = new FileInputStream(fileName);
            reader = new BufferedReader(new InputStreamReader(fis));
            char[] block = new char[512];
            while (true) {
                int readLength = reader.read(block);
                if (readLength == -1)
                    break;// end of file
                result.append(block, 0, readLength);
            }
            return result;
        } catch (Exception fe) {
        	return result;
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException ex) {
            }
        }
	}
	
	protected int deleteRecodeById(String tblName,Integer id){
		String sql = "update " + tblName + " set del_flg = '1' where id = ?/*ID*/";
		return dbutil.doUpdate(sql,new Object[]{id},new Class[]{Integer.class});
	}
}
